Skip to contentMethod: processParticipationConstraint(OWLClass, OWLClassExpression)
1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jopa.owl2java;
16:
17: import java.util.HashMap;
18: import java.util.HashSet;
19: import java.util.Map;
20: import java.util.Set;
21:
22: import cz.cvut.kbss.jopa.model.SequencesVocabulary;
23: import org.semanticweb.owlapi.apibinding.OWLManager;
24: import org.semanticweb.owlapi.model.OWLAnnotationAssertionAxiom;
25: import org.semanticweb.owlapi.model.OWLAnnotationProperty;
26: import org.semanticweb.owlapi.model.OWLAnnotationPropertyDomainAxiom;
27: import org.semanticweb.owlapi.model.OWLAnnotationPropertyRangeAxiom;
28: import org.semanticweb.owlapi.model.OWLAsymmetricObjectPropertyAxiom;
29: import org.semanticweb.owlapi.model.OWLAxiom;
30: import org.semanticweb.owlapi.model.OWLAxiomVisitor;
31: import org.semanticweb.owlapi.model.OWLClass;
32: import org.semanticweb.owlapi.model.OWLClassAssertionAxiom;
33: import org.semanticweb.owlapi.model.OWLClassExpression;
34: import org.semanticweb.owlapi.model.OWLClassExpressionVisitor;
35: import org.semanticweb.owlapi.model.OWLDataAllValuesFrom;
36: import org.semanticweb.owlapi.model.OWLDataExactCardinality;
37: import org.semanticweb.owlapi.model.OWLDataFactory;
38: import org.semanticweb.owlapi.model.OWLDataHasValue;
39: import org.semanticweb.owlapi.model.OWLDataMaxCardinality;
40: import org.semanticweb.owlapi.model.OWLDataMinCardinality;
41: import org.semanticweb.owlapi.model.OWLDataProperty;
42: import org.semanticweb.owlapi.model.OWLDataPropertyAssertionAxiom;
43: import org.semanticweb.owlapi.model.OWLDataPropertyDomainAxiom;
44: import org.semanticweb.owlapi.model.OWLDataPropertyExpression;
45: import org.semanticweb.owlapi.model.OWLDataPropertyRangeAxiom;
46: import org.semanticweb.owlapi.model.OWLDataRange;
47: import org.semanticweb.owlapi.model.OWLDataSomeValuesFrom;
48: import org.semanticweb.owlapi.model.OWLDatatype;
49: import org.semanticweb.owlapi.model.OWLDatatypeDefinitionAxiom;
50: import org.semanticweb.owlapi.model.OWLDeclarationAxiom;
51: import org.semanticweb.owlapi.model.OWLDifferentIndividualsAxiom;
52: import org.semanticweb.owlapi.model.OWLDisjointClassesAxiom;
53: import org.semanticweb.owlapi.model.OWLDisjointDataPropertiesAxiom;
54: import org.semanticweb.owlapi.model.OWLDisjointObjectPropertiesAxiom;
55: import org.semanticweb.owlapi.model.OWLDisjointUnionAxiom;
56: import org.semanticweb.owlapi.model.OWLEntityVisitor;
57: import org.semanticweb.owlapi.model.OWLEquivalentClassesAxiom;
58: import org.semanticweb.owlapi.model.OWLEquivalentDataPropertiesAxiom;
59: import org.semanticweb.owlapi.model.OWLEquivalentObjectPropertiesAxiom;
60: import org.semanticweb.owlapi.model.OWLFunctionalDataPropertyAxiom;
61: import org.semanticweb.owlapi.model.OWLFunctionalObjectPropertyAxiom;
62: import org.semanticweb.owlapi.model.OWLHasKeyAxiom;
63: import org.semanticweb.owlapi.model.OWLInverseFunctionalObjectPropertyAxiom;
64: import org.semanticweb.owlapi.model.OWLInverseObjectPropertiesAxiom;
65: import org.semanticweb.owlapi.model.OWLIrreflexiveObjectPropertyAxiom;
66: import org.semanticweb.owlapi.model.OWLNamedIndividual;
67: import org.semanticweb.owlapi.model.OWLNegativeDataPropertyAssertionAxiom;
68: import org.semanticweb.owlapi.model.OWLNegativeObjectPropertyAssertionAxiom;
69: import org.semanticweb.owlapi.model.OWLObject;
70: import org.semanticweb.owlapi.model.OWLObjectAllValuesFrom;
71: import org.semanticweb.owlapi.model.OWLObjectComplementOf;
72: import org.semanticweb.owlapi.model.OWLObjectExactCardinality;
73: import org.semanticweb.owlapi.model.OWLObjectHasSelf;
74: import org.semanticweb.owlapi.model.OWLObjectHasValue;
75: import org.semanticweb.owlapi.model.OWLObjectIntersectionOf;
76: import org.semanticweb.owlapi.model.OWLObjectMaxCardinality;
77: import org.semanticweb.owlapi.model.OWLObjectMinCardinality;
78: import org.semanticweb.owlapi.model.OWLObjectOneOf;
79: import org.semanticweb.owlapi.model.OWLObjectProperty;
80: import org.semanticweb.owlapi.model.OWLObjectPropertyAssertionAxiom;
81: import org.semanticweb.owlapi.model.OWLObjectPropertyDomainAxiom;
82: import org.semanticweb.owlapi.model.OWLObjectPropertyExpression;
83: import org.semanticweb.owlapi.model.OWLObjectPropertyRangeAxiom;
84: import org.semanticweb.owlapi.model.OWLObjectSomeValuesFrom;
85: import org.semanticweb.owlapi.model.OWLObjectUnionOf;
86: import org.semanticweb.owlapi.model.OWLOntology;
87: import org.semanticweb.owlapi.model.OWLReflexiveObjectPropertyAxiom;
88: import org.semanticweb.owlapi.model.OWLSameIndividualAxiom;
89: import org.semanticweb.owlapi.model.OWLSubAnnotationPropertyOfAxiom;
90: import org.semanticweb.owlapi.model.OWLSubClassOfAxiom;
91: import org.semanticweb.owlapi.model.OWLSubDataPropertyOfAxiom;
92: import org.semanticweb.owlapi.model.OWLSubObjectPropertyOfAxiom;
93: import org.semanticweb.owlapi.model.OWLSubPropertyChainOfAxiom;
94: import org.semanticweb.owlapi.model.OWLSymmetricObjectPropertyAxiom;
95: import org.semanticweb.owlapi.model.OWLTransitiveObjectPropertyAxiom;
96: import org.semanticweb.owlapi.model.SWRLRule;
97:
98: import cz.cvut.kbss.jopa.model.ic.DataParticipationConstraint;
99: import cz.cvut.kbss.jopa.model.ic.DataRangeConstraint;
100: import cz.cvut.kbss.jopa.model.ic.IntegrityConstraint;
101: import cz.cvut.kbss.jopa.model.ic.IntegrityConstraintFactory;
102: import cz.cvut.kbss.jopa.model.ic.ObjectParticipationConstraint;
103: import cz.cvut.kbss.jopa.model.ic.ObjectRangeConstraint;
104: import cz.cvut.kbss.jopa.owl2java.OWL2JavaTransformer.Card;
105: import org.slf4j.Logger;
106: import org.slf4j.LoggerFactory;
107:
108: public class IntegrityConstraintParserImpl implements OWLAxiomVisitor {
109:
110: private static final Logger LOG = LoggerFactory.getLogger(OWL2JavaTransformer.class);
111:
112: private OWLDataFactory f;
113:
114: // private Map<OWLClass, OWLClass> superClasses = new HashMap<OWLClass,
115: // OWLClass>();
116: // private Map<OWLObjectProperty, Set<ObjectDomainConstraint>> odConstraints
117: // = new HashMap<OWLObjectProperty, Set<ObjectDomainConstraint>>();
118: // private Map<OWLDataProperty, Set<DataDomainConstraint>> ddConstraints =
119: // new HashMap<OWLDataProperty, Set<DataDomainConstraint>>();
120: // private Map<OWLObjectProperty, ObjectRangeConstraint> orConstraints = new
121: // HashMap<OWLObjectProperty, ObjectRangeConstraint>();
122: // private Map<OWLObjectProperty, DataRangeConstraint> drConstraints = new
123: // HashMap<OWLObjectProperty, DataRangeConstraint>();
124: // private Map<OWLObjectProperty, ObjectRangeConstraint> orConstraints = new
125: // HashMap<OWLObjectProperty, ObjectRangeConstraint>();
126: // private Map<OWLObjectProperty, DataRangeConstraint> drConstraints = new
127: // HashMap<OWLObjectProperty, DataRangeConstraint>();
128:
129: private Map<OWLClass, Map<OWLObjectProperty, Set<IntegrityConstraint>>> opConstraints = new HashMap<>();
130: private Map<OWLClass, Map<OWLDataProperty, Set<IntegrityConstraint>>> dpConstraints = new HashMap<>();
131:
132: // private Map<OWLClass, Map<OWLDataProperty, Set<IntegrityConstraint>>>
133: // dpConstraints = new HashMap<OWLClass, Map<OWLDataProperty,
134: // Set<IntegrityConstraint>>>();
135:
136: // private Map<OWLObjectProperty,Set<OWLObjectProperty>> inverseProperties =
137: // new HashMap<OWLObjectProperty, Set<OWLObjectProperty>>();
138:
139: private ContextDefinition ctx;
140:
141: public IntegrityConstraintParserImpl(final OWLDataFactory f, final ContextDefinition ctx) {
142: this.ctx = ctx;
143: this.f = f;
144: }
145:
146: public void parse() {
147: for (final OWLAxiom a : ctx.axioms) {
148: a.accept(this);
149: }
150: }
151:
152: private static void notSupported(String message, final OWLObject o) {
153: LOG.info(message + " : " + o);
154: }
155:
156: private static void notSupported(final OWLObject o) {
157: notSupported("Ignoring Unsupported Axiom", o);
158: }
159:
160: public void visit(OWLAnnotationPropertyRangeAxiom axiom) {
161: notSupported(axiom);
162: }
163:
164: public void visit(OWLAnnotationPropertyDomainAxiom axiom) {
165: notSupported(axiom);
166: }
167:
168: public void visit(OWLSubAnnotationPropertyOfAxiom axiom) {
169: notSupported(axiom);
170: }
171:
172: public void visit(OWLAnnotationAssertionAxiom axiom) {
173: notSupported(axiom);
174: }
175:
176: public void visit(SWRLRule axiom) {
177: notSupported(axiom);
178: }
179:
180: public void visit(OWLDatatypeDefinitionAxiom axiom) {
181: notSupported(axiom);
182: }
183:
184: public void visit(OWLHasKeyAxiom axiom) {
185: notSupported(axiom);
186: }
187:
188: public void visit(OWLInverseObjectPropertiesAxiom axiom) {
189: notSupported(axiom);
190: }
191:
192: public void visit(OWLSubPropertyChainOfAxiom axiom) {
193: notSupported(axiom);
194: }
195:
196: public void visit(OWLSameIndividualAxiom axiom) {
197: notSupported(axiom);
198: }
199:
200: public void visit(OWLInverseFunctionalObjectPropertyAxiom axiom) {
201: notSupported(axiom);
202: }
203:
204: public void visit(OWLSubDataPropertyOfAxiom axiom) {
205: notSupported(axiom);
206: }
207:
208: public void visit(OWLIrreflexiveObjectPropertyAxiom axiom) {
209: notSupported(axiom);
210: }
211:
212: public void visit(OWLTransitiveObjectPropertyAxiom axiom) {
213: notSupported(axiom);
214: }
215:
216: public void visit(OWLDataPropertyAssertionAxiom axiom) {
217: notSupported(axiom);
218: }
219:
220: public void visit(OWLEquivalentClassesAxiom axiom) {
221: notSupported(axiom);
222: }
223:
224: public void visit(OWLClassAssertionAxiom axiom) {
225: notSupported(axiom);
226: }
227:
228: public void visit(OWLEquivalentDataPropertiesAxiom axiom) {
229: notSupported(axiom);
230: }
231:
232: public void visit(OWLFunctionalDataPropertyAxiom axiom) {
233: // ic.addAll(processParticipationConstraint(f.getOWLThing(), f
234: // .getOWLDataMaxCardinality(1, axiom.getProperty())));
235:
236: // processParticipationConstraint(f.getOWLThing(), f
237: // .getOWLDataMaxCardinality(1, axiom.getProperty()));
238: notSupported(axiom);
239: }
240:
241: public void visit(OWLDataPropertyRangeAxiom axiom) {
242: notSupported(axiom);
243: }
244:
245: public void visit(OWLSymmetricObjectPropertyAxiom axiom) {
246: notSupported(axiom);
247: }
248:
249: public void visit(OWLDisjointUnionAxiom axiom) {
250: notSupported(axiom);
251: }
252:
253: public void visit(OWLSubObjectPropertyOfAxiom axiom) {
254: notSupported(axiom);
255: }
256:
257: public void visit(OWLFunctionalObjectPropertyAxiom axiom) {
258: // ic.addAll(processParticipationConstraint(f.getOWLThing(), f
259: // .getOWLObjectMaxCardinality(1, axiom.getProperty())));
260:
261: // processParticipationConstraint(f.getOWLThing(), f
262: // .getOWLObjectMaxCardinality(1, axiom.getProperty()));
263: notSupported(axiom);
264: }
265:
266: public void visit(OWLObjectPropertyAssertionAxiom axiom) {
267: notSupported(axiom);
268: }
269:
270: public void visit(OWLObjectPropertyRangeAxiom axiom) {
271: try {
272: // ic.addAll(processParticipationConstraint(f.getOWLThing(), f
273: // .getOWLObjectMaxCardinality(1, axiom.getProperty())));
274:
275: OWLObjectProperty op = ensureObjectProperty(axiom.getProperty());
276: OWLClass clz = ensureClass(axiom.getRange());
277: // ObjectRangeConstraint c = orConstraints.get(op);
278: // if (c == null) {
279: // orConstraints.put(op, IntegrityConstraintFactory
280: // .ObjectPropertyRangeConstraint(f.getOWLThing(), op, clz));
281: // } else {
282: // notSupported("Multiple ranges not supported", axiom);
283: // }
284:
285: processParticipationConstraint(f.getOWLThing(),
286: f.getOWLObjectAllValuesFrom(op, clz));
287: } catch (UnsupportedICException e) {
288: notSupported(axiom);
289: }
290: }
291:
292: public void visit(OWLDisjointObjectPropertiesAxiom axiom) {
293: notSupported(axiom);
294: }
295:
296: public void visit(OWLDisjointDataPropertiesAxiom axiom) {
297: notSupported(axiom);
298: }
299:
300: public void visit(OWLDifferentIndividualsAxiom axiom) {
301: notSupported(axiom);
302: }
303:
304: public void visit(OWLNegativeDataPropertyAssertionAxiom axiom) {
305: notSupported(axiom);
306: }
307:
308: public void visit(OWLEquivalentObjectPropertiesAxiom axiom) {
309: notSupported(axiom);
310: }
311:
312: public void visit(OWLObjectPropertyDomainAxiom axiom) {
313: // OWLObjectProperty op = ensureObjectProperty(axiom.getProperty());
314: // OWLClass clz = ensureClass(axiom.getDomain());
315: // Set<ObjectDomainConstraint> c = odConstraints.get(op);
316: // if (c == null) {
317: // c = new HashSet<ObjectDomainConstraint>();
318: // odConstraints.put(op, c);
319: // }
320: //
321: // c.add(IntegrityConstraintFactory
322: // .ObjectPropertyDomainConstraint(op, clz));
323: notSupported(axiom);
324: }
325:
326: public void visit(OWLDataPropertyDomainAxiom axiom) {
327: // OWLDataProperty op = ensureDataProperty(axiom.getProperty());
328: // OWLClass clz = ensureClass(axiom.getDomain());
329: // Set<DataDomainConstraint> c = ddConstraints.get(op);
330: // if (c == null) {
331: // c = new HashSet<DataDomainConstraint>();
332: // ddConstraints.put(op, c);
333: // }
334: //
335: // c.add(IntegrityConstraintFactory.DataPropertyDomainConstraint(op,
336: // clz));
337: notSupported(axiom);
338: }
339:
340: public void visit(OWLDisjointClassesAxiom axiom) {
341: notSupported(axiom);
342: }
343:
344: public void visit(OWLReflexiveObjectPropertyAxiom axiom) {
345: notSupported(axiom);
346: }
347:
348: public void visit(OWLAsymmetricObjectPropertyAxiom axiom) {
349: notSupported(axiom);
350: }
351:
352: public void visit(OWLNegativeObjectPropertyAssertionAxiom axiom) {
353: notSupported(axiom);
354: }
355:
356: public void visit(OWLSubClassOfAxiom axiom) {
357: try {
358: if (!axiom.getSubClass().isAnonymous()
359: && !axiom.getSuperClass().isAnonymous()) {
360: // OWLClass supC = superClasses.get(axiom.getSubClass());
361: // if (supC == null) {
362: // superClasses.put(axiom.getSubClass().asOWLClass(), axiom
363: // .getSuperClass().asOWLClass());
364: // } else {
365: // notSupported(
366: // "Multiple inheritance not allowed in Java OO model",
367: // axiom);
368: // }
369: notSupported(axiom);
370: } else if (!axiom.getSubClass().isAnonymous()) {
371: processParticipationConstraint(axiom.getSubClass().asOWLClass(),
372: axiom.getSuperClass());
373: } else {
374: notSupported(axiom);
375: }
376: } catch (UnsupportedICException e) {
377: notSupported(axiom);
378: }
379: }
380:
381: public void visit(final OWLDeclarationAxiom axiom) {
382: axiom.getEntity().accept(new OWLEntityVisitor() {
383:
384: public void visit(OWLAnnotationProperty property) {
385: ctx.annotationProperties.add(property);
386: }
387:
388: public void visit(OWLDatatype datatype) {
389: notSupported(axiom);
390: }
391:
392: public void visit(OWLNamedIndividual individual) {
393: notSupported(axiom);
394: }
395:
396: public void visit(OWLDataProperty property) {
397: ctx.dataProperties.add(property);
398: }
399:
400: public void visit(OWLObjectProperty property) {
401: ctx.objectProperties.add(property);
402: }
403:
404: public void visit(OWLClass cls) {
405: ctx.classes.add(cls);
406: }
407: });
408: }
409:
410: private OWLDatatype ensureDatatype(final OWLDataRange r)
411: throws UnsupportedICException {
412: if (!r.isDatatype()) {
413: throw new UnsupportedICException("Data ranges not supported: " + r);
414: }
415:
416: if (!r.asOWLDatatype().isBuiltIn()) {
417: throw new UnsupportedICException(
418: "Only built in datatypes are supported: " + r);
419: }
420:
421: return r.asOWLDatatype();
422: }
423:
424: private static OWLClass ensureClass(final OWLClassExpression r) {
425: if (!r.isAnonymous()) {
426: return r.asOWLClass();
427: }
428: throw new UnsupportedICException("Only named classes are supported: " + r);
429: }
430:
431: // private void ensureNamedIndividual(final OWLIndividual r)
432: // throws RuntimeException {
433: // if (r.isAnonymous()) {
434: // throw new RuntimeException("Anonymous individuals not supported: "
435: // + r);
436: // }
437: // }
438:
439: private static OWLDataProperty ensureDataProperty(final OWLDataPropertyExpression e) {
440: if (e.isAnonymous()) {
441: throw new UnsupportedICException(
442: "Data property expressions not supported: " + e);
443: }
444:
445: return e.asOWLDataProperty();
446: }
447:
448: private static OWLObjectProperty ensureObjectProperty(
449: final OWLObjectPropertyExpression e) throws UnsupportedICException {
450: if (e.isAnonymous()) {
451: throw new UnsupportedICException(
452: "Object property expressions not supported: " + e);
453: }
454:
455: return e.asOWLObjectProperty();
456: }
457:
458: private void processParticipationConstraint(final OWLClass subjClass, final OWLClassExpression superClass) {
459: Map<OWLObjectProperty, Set<IntegrityConstraint>> setOP2 = opConstraints
460: .get(subjClass);
461:• if (setOP2 == null) {
462: setOP2 = new HashMap<OWLObjectProperty, Set<IntegrityConstraint>>();
463: opConstraints.put(subjClass, setOP2);
464: }
465: Map<OWLDataProperty, Set<IntegrityConstraint>> setDP2 = dpConstraints
466: .get(subjClass);
467:• if (setDP2 == null) {
468: setDP2 = new HashMap<OWLDataProperty, Set<IntegrityConstraint>>();
469: dpConstraints.put(subjClass, setDP2);
470: }
471:
472: // final Map<OWLObjectProperty, Set<ObjectParticipationConstraint>>
473: // mapOP = setOP2;
474: // final Map<OWLDataProperty, Set<DataParticipationConstraint>> mapDP =
475: // setDP2;
476: final Map<OWLObjectProperty, Set<IntegrityConstraint>> mapOP = setOP2;
477: final Map<OWLDataProperty, Set<IntegrityConstraint>> mapDP = setDP2;
478:
479: final OWLClassExpressionVisitor v = new OWLClassExpressionVisitor() {
480:
481: public void visit(OWLDataMaxCardinality arg0) {
482: try {
483: final OWLDatatype dt = ensureDatatype(arg0.getFiller());
484: final OWLDataProperty dp = ensureDataProperty(arg0
485: .getProperty());
486:
487: Set<IntegrityConstraint> dpc = mapDP.get(dp);
488: if (dpc == null) {
489: dpc = new HashSet<IntegrityConstraint>();
490: mapDP.put(dp, dpc);
491: }
492:
493: dpc.add(IntegrityConstraintFactory
494: .MaxDataParticipationConstraint(subjClass, dp, dt,
495: arg0.getCardinality()));
496: } catch (UnsupportedICException e) {
497: notSupported(arg0);
498: }
499: }
500:
501: public void visit(OWLDataExactCardinality arg0) {
502: try {
503: final OWLDatatype dt = ensureDatatype(arg0.getFiller());
504: final OWLDataProperty dp = ensureDataProperty(arg0
505: .getProperty());
506:
507: Set<IntegrityConstraint> dpc = mapDP.get(dp);
508: if (dpc == null) {
509: dpc = new HashSet<IntegrityConstraint>();
510: mapDP.put(dp, dpc);
511: }
512:
513: dpc.add(IntegrityConstraintFactory.DataParticipationConstraint(
514: subjClass, dp, dt, arg0.getCardinality(),
515: arg0.getCardinality()));
516: } catch (UnsupportedICException e) {
517: notSupported(arg0);
518: }
519: }
520:
521: // private void processDataCardinality(final int minCardinality,
522: // final int maxCardinality, OWLDataProperty p,
523: // final OWLDatatype c) {
524: //
525: // final Set<OWLDatatype> validRanges = new HashSet<OWLDatatype>();
526: //
527: // for (final OWLDatatype cc : o.getDatatypesInSignature()) {
528: // if (r.isEntailed(f.getOWLDataPropertyRangeAxiom(p, cc))) {
529: // validRanges.add(cc);
530: // }
531: // }
532: //
533: // if (validRanges.isEmpty()) {
534: // validRanges.add(c);
535: // }
536: //
537: // // infer cardinality
538: // for (final OWLDatatype cc : validRanges) {
539: // if ((maxCardinality > 1)
540: // && r.isEntailed(f.getOWLSubClassOfAxiom(subjClass,
541: // f.getOWLDataMaxCardinality(1, p
542: // .asOWLDataProperty(), cc)))
543: // || r.isEntailed(f
544: // .getOWLFunctionalDataPropertyAxiom(p
545: // .asOWLDataProperty()))) {
546: //
547: // set.add(IntegrityConstraintFactory
548: // .datatypeParticipationConstraint(subjClass, p,
549: // cc, minCardinality, 1));
550: // } else {
551: // // must be consistent
552: //
553: // set.add(IntegrityConstraintFactory
554: // .datatypeParticipationConstraint(subjClass, p,
555: // cc, minCardinality, maxCardinality));
556: // }
557: // }
558: // }
559:
560: public void visit(OWLDataMinCardinality arg0) {
561: try {
562: final OWLDatatype dt = ensureDatatype(arg0.getFiller());
563: final OWLDataProperty dp = ensureDataProperty(arg0
564: .getProperty());
565:
566: Set<IntegrityConstraint> dpc = mapDP.get(dp);
567: if (dpc == null) {
568: dpc = new HashSet<IntegrityConstraint>();
569: mapDP.put(dp, dpc);
570: }
571:
572: dpc.add(IntegrityConstraintFactory
573: .MinDataParticipationConstraint(subjClass, dp, dt,
574: arg0.getCardinality()));
575: } catch (UnsupportedICException e) {
576: notSupported(arg0);
577: }
578: }
579:
580: public void visit(OWLDataHasValue arg0) {
581: notSupported(arg0);
582: // ensureDataProperty(arg0.getProperty());
583: //
584: // set.add(IntegrityConstraintFactory
585: // .datatypeParticipationConstraint(subjClass, arg0
586: // .getProperty().asOWLDataProperty(), f
587: // .getOWLDataOneOf(arg0.getValue()), 1, 1));
588: }
589:
590: public void visit(OWLDataAllValuesFrom arg0) {
591: try {
592: OWLDataProperty op = ensureDataProperty(arg0.getProperty());
593: OWLDatatype clz = ensureDatatype(arg0.getFiller());
594:
595: Set<IntegrityConstraint> opc = mapDP.get(op);
596: if (opc == null) {
597: opc = new HashSet<IntegrityConstraint>();
598: mapDP.put(op, opc);
599: }
600: opc.add(IntegrityConstraintFactory.DataPropertyRangeConstraint(
601: subjClass, op, clz));
602: } catch (UnsupportedICException e) {
603: notSupported(arg0);
604: }
605: }
606:
607: public void visit(OWLDataSomeValuesFrom arg0) {
608: try {
609: final OWLDatatype dt = ensureDatatype(arg0.getFiller());
610: final OWLDataProperty dp = ensureDataProperty(arg0
611: .getProperty());
612:
613: Set<IntegrityConstraint> dpc = mapDP.get(dp);
614: if (dpc == null) {
615: dpc = new HashSet<IntegrityConstraint>();
616: mapDP.put(dp, dpc);
617: }
618:
619: dpc.add(IntegrityConstraintFactory
620: .MinDataParticipationConstraint(subjClass, dp, dt, 1));
621: } catch (UnsupportedICException e) {
622: notSupported(arg0);
623: }
624: }
625:
626: public void visit(OWLObjectOneOf arg0) {
627: notSupported(arg0);
628: }
629:
630: public void visit(OWLObjectHasSelf arg0) {
631: notSupported(arg0);
632: }
633:
634: public void visit(OWLObjectMaxCardinality arg0) {
635: try {
636: OWLClass c = ensureClass(arg0.getFiller());
637: OWLObjectProperty p = ensureObjectProperty(arg0.getProperty());
638:
639: Set<IntegrityConstraint> opc = mapOP.get(p);
640: if (opc == null) {
641: opc = new HashSet<IntegrityConstraint>();
642: mapOP.put(p, opc);
643: }
644:
645: opc.add(IntegrityConstraintFactory
646: .MaxObjectParticipationConstraint(subjClass, p, c,
647: arg0.getCardinality()));
648: } catch (UnsupportedICException e) {
649: notSupported(arg0);
650: }
651: }
652:
653: public void visit(OWLObjectExactCardinality arg0) {
654: try {
655: OWLClass c = ensureClass(arg0.getFiller());
656: OWLObjectProperty p = ensureObjectProperty(arg0.getProperty());
657:
658: Set<IntegrityConstraint> opc = mapOP.get(p);
659: if (opc == null) {
660: opc = new HashSet<IntegrityConstraint>();
661: mapOP.put(p, opc);
662: }
663:
664: opc.add(IntegrityConstraintFactory
665: .ObjectParticipationConstraint(subjClass, p, c,
666: arg0.getCardinality(), arg0.getCardinality()));
667: } catch (UnsupportedICException e) {
668: notSupported(arg0);
669: }
670: }
671:
672: // private void processObjectMinCardinality(final int cardinality,
673: // OWLObjectProperty p, final OWLClass c) {
674: //
675: // // infer range
676: // final NodeSet<OWLClass> nSet = r.getObjectPropertyRanges(p,
677: // false);
678: //
679: // final Set<OWLClass> validRanges = new HashSet<OWLClass>();
680: //
681: // for (final OWLClass cc : nSet.getFlattened()) {
682: // if (r.isEntailed(f.getOWLSubClassOfAxiom(cc, c))) {
683: // validRanges.add(cc);
684: // }
685: // }
686: //
687: // if (validRanges.isEmpty()) {
688: // validRanges.add(c);
689: // }
690: //
691: // // infer cardinality
692: // for (final OWLClass cc : validRanges) {
693: // if (r.isEntailed(f.getOWLSubClassOfAxiom(subjClass, f
694: // .getOWLObjectMaxCardinality(cardinality, p
695: // .asOWLObjectProperty(), cc)))
696: // || r.isEntailed(f
697: // .getOWLFunctionalObjectPropertyAxiom(p
698: // .asOWLObjectProperty()))) {
699: // set.add(IntegrityConstraintFactory
700: // .classParticipationConstraint(subjClass, p, cc,
701: // cardinality, cardinality));
702: // } else {
703: // // must be consistent
704: // set.add(IntegrityConstraintFactory
705: // .classParticipationConstraint(subjClass, p, cc,
706: // cardinality, Integer.MAX_VALUE));
707: // }
708: // }
709: // }
710:
711: public void visit(OWLObjectMinCardinality arg0) {
712: try {
713: OWLClass c = ensureClass(arg0.getFiller());
714: OWLObjectProperty p = ensureObjectProperty(arg0.getProperty());
715:
716: Set<IntegrityConstraint> opc = mapOP.get(p);
717: if (opc == null) {
718: opc = new HashSet<IntegrityConstraint>();
719: mapOP.put(p, opc);
720: }
721:
722: opc.add(IntegrityConstraintFactory
723: .MinObjectParticipationConstraint(subjClass, p, c,
724: arg0.getCardinality()));
725: } catch (UnsupportedICException e) {
726: notSupported(arg0);
727: }
728: }
729:
730: public void visit(OWLObjectHasValue arg0) {
731: notSupported(arg0);
732: // ensureObjectProperty(arg0.getProperty());
733: // ensureNamedIndividual(arg0.getValue());
734: //
735: // set.add(IntegrityConstraintFactory
736: // .classParticipationConstraint(subjClass, arg0
737: // .getProperty().asOWLObjectProperty(), f
738: // .getOWLObjectOneOf(arg0.getValue()
739: // .asOWLNamedIndividual()), 1, 1));
740: }
741:
742: public void visit(OWLObjectAllValuesFrom arg0) {
743: try {
744: // System.out.println("Found ICX: " + c);
745: OWLObjectProperty op = ensureObjectProperty(arg0.getProperty());
746: OWLClass clz = ensureClass(arg0.getFiller());
747:
748: Set<IntegrityConstraint> opc = mapOP.get(op);
749: if (opc == null) {
750: opc = new HashSet<IntegrityConstraint>();
751: mapOP.put(op, opc);
752: }
753: opc.add(IntegrityConstraintFactory
754: .ObjectPropertyRangeConstraint(subjClass, op, clz));
755: } catch (UnsupportedICException e) {
756: notSupported(arg0);
757: }
758: }
759:
760: public void visit(OWLObjectSomeValuesFrom arg0) {
761: try {
762: OWLClass c = ensureClass(arg0.getFiller());
763: OWLObjectProperty p = ensureObjectProperty(arg0.getProperty());
764:
765: Set<IntegrityConstraint> opc = mapOP.get(p);
766: if (opc == null) {
767: opc = new HashSet<IntegrityConstraint>();
768: mapOP.put(p, opc);
769: }
770:
771: opc.add(IntegrityConstraintFactory
772: .MinObjectParticipationConstraint(subjClass, p, c, 1));
773: } catch (UnsupportedICException e) {
774: notSupported(arg0);
775: }
776: }
777:
778: public void visit(OWLObjectComplementOf arg0) {
779: notSupported(arg0);
780: }
781:
782: public void visit(OWLObjectUnionOf arg0) {
783: notSupported(arg0);
784: }
785:
786: public void visit(OWLObjectIntersectionOf arg0) {
787: for (final OWLClassExpression e : arg0.getOperands()) {
788: e.accept(this);
789: }
790: }
791:
792: public void visit(OWLClass arg0) {
793: notSupported(arg0);
794: }
795: };
796: superClass.accept(v);
797: }
798:
799: // public OWLClass getSuperClass(final OWLClass subClass) {
800: // OWLClass superClass = superClasses.get(subClass);
801: // // if (superClass == null) {
802: // // superClass = f.getOWLThing();
803: // // }
804: // return superClass;
805: // }
806:
807: interface ClassDataPropertyComputer {
808:
809: public Card getCard();
810:
811: public OWLDatatype getFiller();
812:
813: public Set<DataParticipationConstraint> getParticipationConstraints();
814: }
815:
816:
817: public ClassDataPropertyComputer getClassDataPropertyComputer(
818: final OWLClass clazz, final OWLDataProperty prop,
819: final OWLOntology merged) {
820: final Set<IntegrityConstraint> ics = new HashSet<IntegrityConstraint>();
821:
822: final Map<OWLDataProperty, Set<IntegrityConstraint>> constraints = dpConstraints
823: .get(clazz);
824: if (constraints != null && constraints.containsKey(prop)) {
825: ics.addAll(constraints.get(prop));
826: }
827:
828: return new ClassDataPropertyComputer() {
829:
830: public Card getCard() {
831: if (ics.isEmpty()) {
832: return Card.NO;
833: }
834:
835: for (DataParticipationConstraint opc : getParticipationConstraints()) {
836: if (!(clazz.equals(opc.getSubject()) && prop.equals(opc
837: .getPredicate()))) {
838: continue;
839: }
840:
841: OWLDatatype dt1 = getFiller();
842: OWLDatatype dt2 = opc.getObject();
843: if (dt1.equals(dt2)
844: || dt2.equals(OWLManager.getOWLDataFactory()
845: .getTopDatatype())) {
846: if (opc.getMax() == 1) {
847: return Card.ONE;
848: }
849: }
850: }
851: return Card.MULTIPLE;
852: }
853:
854: public OWLDatatype getFiller() {
855: for (final IntegrityConstraint ic : ics) {
856: if (ic instanceof DataRangeConstraint) {
857: return ((DataRangeConstraint) ic).getRange();
858: }
859: }
860:
861: return f.getRDFPlainLiteral(); // f.getTopDatatype();
862: }
863:
864: public Set<DataParticipationConstraint> getParticipationConstraints() {
865: Set<DataParticipationConstraint> set = new HashSet<DataParticipationConstraint>();
866: for (final IntegrityConstraint ic : ics) {
867: if (ic instanceof DataParticipationConstraint) {
868: set.add((DataParticipationConstraint) ic);
869: }
870: }
871:
872: return set;
873: }
874: };
875:
876: }
877:
878: class ClassObjectPropertyComputer {
879:
880: private Card card;
881:
882: private OWLClass object;
883:
884: private Set<ObjectParticipationConstraint> ics;
885:
886: ClassObjectPropertyComputer(final OWLClass clazz, final OWLObjectProperty prop,
887: final OWLOntology merged) {
888: final Map<OWLObjectProperty, Set<IntegrityConstraint>> constraints = opConstraints
889: .get(clazz);
890: ics = new HashSet<>();
891: if (constraints != null && constraints.containsKey(prop)) {
892: OWLClass filler = null;
893: for (final IntegrityConstraint ic : constraints.get(prop)) {
894:
895: if (ic instanceof ObjectParticipationConstraint) {
896: ics.add((ObjectParticipationConstraint) ic);
897: } else if (ic instanceof ObjectRangeConstraint) {
898: filler = ((ObjectRangeConstraint) ic).getRange();
899: }
900: }
901:
902: if (filler == null) {
903: filler = f.getOWLThing();
904: }
905:
906: object = filler;
907:
908: final OWLDataFactory f = merged.getOWLOntologyManager().getOWLDataFactory();
909:
910: if (filler.getSuperClasses(merged).contains(
911: f.getOWLClass(org.semanticweb.owlapi.model.IRI.create(SequencesVocabulary.c_List)))) {
912: object = new ClassObjectPropertyComputer(filler, f.getOWLObjectProperty(
913: org.semanticweb.owlapi.model.IRI.create(SequencesVocabulary.p_element)), merged)
914: .getObject();
915: card = Card.LIST;
916: } else if (filler.getSuperClasses(merged).contains(
917: f.getOWLClass(org.semanticweb.owlapi.model.IRI.create(SequencesVocabulary.c_OWLSimpleList)))) {
918: object = new ClassObjectPropertyComputer(filler, f.getOWLObjectProperty(
919: org.semanticweb.owlapi.model.IRI.create(SequencesVocabulary.p_hasNext)), merged)
920: .getObject();
921: card = Card.SIMPLELIST; // TODO referenced
922: } else {
923: card = Card.MULTIPLE;
924: for (ObjectParticipationConstraint opc : ics) {
925: OWLClass dt2 = opc.getObject();
926: if (filler.equals(dt2)
927: || dt2.equals(OWLManager.getOWLDataFactory()
928: .getOWLThing())) {
929: if (opc.getMax() == 1) {
930: card = Card.ONE;
931: break;
932: }
933: }
934: }
935: }
936: } else {
937: card = Card.NO;
938: }
939: }
940:
941: public Card getCard() {
942: return card;
943: }
944:
945: public OWLClass getObject() {
946: return object;
947: }
948:
949: public Set<ObjectParticipationConstraint> getParticipationConstraints() {
950: return ics;
951: }
952: }
953: }
954:
955: